library(ggplot2)

######################################
## Plot histograms of first saccades after onset of linguistic event of interest by region
## created by jdegen on 06/19/2013
## Exclusion criteria (taken from Salverda, Kleinschmidt, and Tanenhaus, under review): a) no saccade was generated after the onset of the target word (2.3% in SKT); b) first saccade following the onset of the target word was directed to a region that the participant was already fixating (23.9% of remaining data in SKT); c) first saccade following onset of target word was not directed to either target or competitor (50.6% in SKT)
## In SKT, 36.8% of trials were retained and used in the analysis
######################################

######################################
# Step 1. Get all the first saccades
######################################
getFirstSaccades <- function(
  d, # data.frame
  timevar, # time variable where zero point is onset of linguistic event of interest
  eyeeventvar=NULL, # variable coding whether eye event is fixation, saccade, blink; if NULL, all events are assumed to be saccades; if not NULL, saccade event must be coded as "SACC"
  trialdataidvar, # variable coding unique trial ID
  dataidvar # variable coding each unique sample
)
{
  delay = d[d[,timevar] > 0,]
  if (! is.null(eyeeventvar))
  {
    delay = delay[delay[,eyeeventvar] == "SACC",]
  }
  tmp = delay[,c(trialdataidvar, dataidvar)]
  tmp = droplevels(tmp)
  # get unique sample ID of first saccade
  t = tapply(as.character(tmp[,dataidvar]),tmp[,trialdataidvar],FUN="[",1)
  nrow(t)
  row.names(delay) = delay[,dataidvar]
  return(delay[t,])
}

######################################
# Step 2. Exclude trials based on SKT criteria
######################################

excludeTrials <- function(
  d, # data.frame with all data
  d.sacc, # data.frame with only first saccades (output of getFirstSaccades() call)
  timevar, # time variable where zero point is onset of linguistic event of interest
  trialdataidvar, # variable coding unique trial ID
  dataidvar, # variable coding each unique sample 
  regionvar, # variable coding region looked to on that sample
  otherregions = c("center","ci","ti","none"), # regions to be excluded from analysis (values of regionvar)
  analysisregions = c("c","t") # target and competitor regions (values of regionvar)
)
{
  print("percent of trials excluded because there were no new saccades after word onset:")
  print(1-(nrow(d.sacc)/length(unique(d[,trialdataidvar]))))
## add information about look on the sample directly prior to the first saccade (prevSampleRegion) and at word onset (FixWordOnset)
#d$prevSampleRegion = as.factor(c(NA,as.character(d[1:(nrow(d)-1),]$rp_RegionType)))
  row.names(d) = d[,dataidvar]
  wordonset = d[d[,timevar] == 0,]
  wordonset = droplevels(wordonset)
  d.sacc$prevSampleRegion = d[as.character(d.sacc$DataID),]$prevSampleRegion
  wordonset = wordonset[wordonset[,trialdataidvar] %in% d.sacc[,trialdataidvar],]
  d.sacc$FixWordOnset = wordonset[,regionvar]
  print("number of trials before exclusion:")
  print(nrow(d.sacc))
  
  ## exclude trials where first saccade is to a region the subject is already fixating
  out.already=d.sacc[as.character(d.sacc[,regionvar])==as.character(d.sacc$FixWordOnset),]
  d.sacc=d.sacc[as.character(d.sacc[,regionvar])!=as.character(d.sacc$FixWordOnset),]
  print("percent of trials excluded because first saccade is to a region the participant was already fixating:")
  print(nrow(out.already)/(nrow(d.sacc) + nrow(out.already)))
  
  out.other = d.sacc[d.sacc[,regionvar] %in% otherregions,]
  d.sacc = d.sacc[d.sacc[,regionvar] %in% analysisregions,]
  d.sacc = droplevels(d.sacc)
  print("percent of trials excluded because first saccade was not to target or competitor:")
  print(nrow(out.other)/(nrow(d.sacc) + nrow(out.other)))
  print("remaining trials after exclusion:")
  print(nrow(d.sacc))
  return(d.sacc)
}

########################################## 
# Step 3. Generate saccade histograms for each condition
########################################## 
# see example_plot_saccades.R
# no gain in generalized function

######################################
## Step 4. Moving window analysis to determine first time window in which probability of saccade to target is different from probability of saccade to competitor
######################################
# see example_plot_saccades.R
# no gain in generalized function

